﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace SafeSave
{
    public partial class ApplyRightForm : CCSkinMain
    {
        public delegate void continueRequest();//声明一个委托
        //声明一个委托类型的事件
        public event continueRequest continueSendRequest;
        string userAddress;
        List<string> policy = new List<string>();
        public ApplyRightForm(string address,string time,string ex_msg)
        {
            InitializeComponent();
            applyAddress.Text = address;
            userAddress = address;
            applyMsg.Text = ex_msg;
        }

        private void confirmButton_Click(object sender, EventArgs e)
        {
            byte[] temp;
            if (vipCheck.Checked)
            {
                temp = System.Text.Encoding.UTF8.GetBytes("VIP");
                policy.Add(Convert.ToBase64String(temp));
            }
            if (homeCheck.Checked)
            {
                temp = System.Text.Encoding.UTF8.GetBytes("家庭");
                policy.Add(Convert.ToBase64String(temp));
            }
            if (schoolCheck.Checked)
            {
                temp = System.Text.Encoding.UTF8.GetBytes("学校");
                policy.Add(Convert.ToBase64String(temp));
            }
            if (workCheck.Checked)
            {
                temp = System.Text.Encoding.UTF8.GetBytes("工作");
                policy.Add(Convert.ToBase64String(temp));
            }
            if (secretCheck.Checked)
            {
                temp = System.Text.Encoding.UTF8.GetBytes("私密");
                policy.Add(Convert.ToBase64String(temp));
            }

            AurthorizeJobject aurthorizeJobject = new AurthorizeJobject();
            aurthorizeJobject.address = userAddress;
            if(policy==null)
            {
                aurthorizeJobject.agree = false;
            }
            else
            {
                aurthorizeJobject.agree = true;
            }
            aurthorizeJobject.attributes = policy;

            string req = JsonConvert.SerializeObject(aurthorizeJobject);//创建请求Json
            HttpHelper httphelper = new HttpHelper();//http辅助类
            httphelper.HttpPost("http://" + User.user.url + "/handle_registrations", req);//返回的Json

            continueSendRequest();
            this.Close();
        }

        private void disagreeButton_Click(object sender, EventArgs e)
        {
            AurthorizeJobject aurthorizeJobject = new AurthorizeJobject();
            aurthorizeJobject.address = userAddress;
            aurthorizeJobject.agree = false;
            aurthorizeJobject.attributes = policy;

            string req = JsonConvert.SerializeObject(aurthorizeJobject);//创建请求Json
            HttpHelper httphelper = new HttpHelper();//http辅助类
            httphelper.HttpPost("http://" + User.user.url + "/handle_registrations", req);//返回的Json

            continueSendRequest();
            this.Close();
        }
    }
}
